﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta><style>/*<![CDATA[*/

table{border: 1px solid gray;}
td{border: 1px dotted gray;}
p{margin: 3px 0 3px 0; padding: 0;}
#ID_Footer{font-size: small; font-style: italic; text-align: right; margin-top: 4em; padding-top: 4px; border-top: 2px solid gray;}

/*]]>*/</style><title>第3章 使用数组</title></head><body>
<div><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">第</span><span style="font-family: Consolas; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">3</span><span style="font-family: 微软雅黑; font-size: 18pt; font-weight: bold; padding-left: 49pt; line-height: 190%">章 &nbsp; &nbsp; &nbsp; &nbsp;使用数组</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">支持：</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">索引数组</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%"> &amp; </span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">关联数组</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">。</span></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">什么是数组</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">(p55)</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">标量变量</span><span style="font-family: Consolas; font-size: 12pt; line-height: 190%"> &amp; </span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">数组</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 190%">索引</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">（</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 190%">关键字</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">）：用来访问数组中的元素。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">允许</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">间隔性地</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">使用</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">数字</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">或</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">字符串</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">作为数组的索引。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">数字索引数组</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">(p56)</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">数字索引默认</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">从</span><span style="font-family: Times New Roman; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">0</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">开始</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">3.2.1 </span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">数字索引数组的初始化</span></div>
<div>
<ol>
<li><span style="font-family: 宋体; font-size: 12pt">利用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">array()</span><span style="font-family: Consolas; font-size: 12pt">(</span><span style="font-family: 宋体; font-size: 12pt">语言结构，非函数，类似</span><span style="font-family: Consolas; font-size: 12pt">echo)</span><span style="font-family: 宋体; font-size: 12pt">初始化：</span></li></ol>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">$products = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 240%">array</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">( &apos;Tires&apos;, &apos;Oil&apos;, &apos;Spark Plugs&apos; );</span></div>
<div>
<ol>
<li><span style="font-family: 宋体; font-size: 12pt">运算符</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000">&quot;</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">=</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000">&quot;</span><span style="font-family: 宋体; font-size: 12pt">，将数组复制到另一个数组</span></li></ol>
<div>
<ol>
<li><span style="font-family: 宋体; font-size: 12pt">利用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">range()</span><span style="font-family: 宋体; font-size: 12pt">函数创建升序排列的数字</span><span style="font-family: Consolas; font-size: 12pt">/</span><span style="font-family: 宋体; font-size: 12pt">字符数组：</span></li></ol>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">$numbers = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #c00000; padding-left: 21pt">range</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">(1, 10);</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt">$odds = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #c00000; padding-left: 21pt">range</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt">(1, 10, 2);</span><span style="font-family: Times New Roman; font-size: 11pt; color: #2f5496; padding-left: 21pt"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt">// </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt">step</span><span style="font-family: Consolas; font-size: 11pt; color: #ff0000; padding-left: 21pt"> </span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt">= 2</span><span style="font-family: 宋体; font-size: 11pt; color: #2f5496; padding-left: 21pt">，</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt">can be negative</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">$letters = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #c00000; padding-left: 21pt; line-height: 240%">range</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt; line-height: 240%">(&apos;a&apos;, &apos;z&apos;);</span></div>
<div>
<ol>
<li><span style="font-family: 宋体; font-size: 12pt">可以直接从</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold">文件</span><span style="font-family: 宋体; font-size: 12pt">或</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold">数据库</span><span style="font-family: 宋体; font-size: 12pt">中直接载入数组</span></li></ol>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">3.2.2 </span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">访问数组的内容</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">(p57)</span></div>
<div>
<ol>
<li><span style="font-family: 宋体; font-size: 12pt">访问</span><span style="font-family: 宋体; font-size: 12pt">数组：</span><span style="font-family: Consolas; font-size: 12pt">$products[0]</span></li>
<li><span style="font-family: 宋体; font-size: 12pt; font-weight: bold">改变</span><span style="font-family: 宋体; font-size: 12pt">数组内容：</span><span style="font-family: Consolas; font-size: 12pt">$products[1] = &apos;Fuses&apos;;</span></li></ol>
<div>
<ol>
<li><span style="font-family: 宋体; font-size: 12pt; font-weight: bold">增加</span><span style="font-family: 宋体; font-size: 12pt">一个元素到数组末尾：</span><span style="font-family: Consolas; font-size: 12pt">$products[3] = &apos;Uber&apos;; //</span><span style="font-family: 宋体; font-size: 12pt">原数组大小为</span><span style="font-family: Consolas; font-size: 12pt">3</span></li></ol>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">数组不需要预先初始化或创建，第一次使用时</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 190%">自动创建</span><span style="font-family: 宋体; font-size: 12pt; line-height: 190%">，即</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">$products[0] = &apos;Tires&apos;;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">$products[1] = &apos;Oil&apos;;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">$products[2] = &apos;Spark Plugs&apos;;</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">3.2.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用循环访问数组</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">(p57)</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">for</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">循环：</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">for ($i = 0; $i &lt; 3; $i++)</span><span style="font-family: Times New Roman; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">{</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">echo $products[$i].&quot; &quot;; }</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 170%">foreach</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">循环：</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(p58)</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #2f5496; padding-left: 21pt">foreach</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt"> ($products </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt">as</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">$current){</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">echo $current.&quot; &quot;; }</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">使用不同索引的数组</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; color: #ff0000; line-height: 170%">索引</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">数组</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">：每个元素一个默认的索引（</span><span style="font-family: Times New Roman; font-size: 12pt; line-height: 170%">0</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">、</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">1</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">等）。</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">关联</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">数组</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">：将变量值与任何</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">关键字</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">或</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">索引</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">关联起来。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 190%">3.3.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 190%">初始化关联数组</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 190%">(p58)</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">$prices = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">array</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">(&apos;Tires&apos;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 240%">=&gt;</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">100, &apos;Oil&apos;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 240%">=&gt;</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">10, &apos;Spark Plugs&apos;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 240%">=&gt;</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">4);</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; padding-left: 21pt; line-height: 240%">关键字</span><span style="font-family: 宋体; font-size: 12pt; padding-left: 21pt; line-height: 240%">和</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; padding-left: 21pt; line-height: 240%">值</span><span style="font-family: 宋体; font-size: 12pt; padding-left: 21pt; line-height: 240%">之间：在大于符号前面加上等于符号，即</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 240%">=&gt;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; color: #000000; line-height: 190%">3.3.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 190%">访问数组元素</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">访问</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">数组</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">$prices[&apos;Tires&apos;]</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">创建</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">并添加元素：</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">$prices = array(&apos;Tires&apos;=&gt;100);</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt">$prices[&apos;Oil&apos;] = 10;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">$prices[&apos;Spark Plugs&apos;] = 4;</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">添加</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">第一个元素的同时</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">创建</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">数组：</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">$prices[&apos;Tires&apos;] = 100;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt">$prices[&apos;Oil&apos;] = 10;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">$prices[&apos;Spark Plugs&apos;] = 4;</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">3.3.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用循环语句</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">(p58)</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">可以使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">foreach</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">循环</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">或</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">list()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">或</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">each()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">结构，无法使用</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">for</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">循环。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">foreach($prices </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%">as</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">$key</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%">=&gt;</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">$value)</span><span style="font-family: Times New Roman; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">{</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">echo $key.</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 21pt; line-height: 160%">&quot;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> = </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 21pt; line-height: 160%">&quot;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">.$value.&quot;&lt;br /&gt;;</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">each()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">结构：</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(p59)</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">while ($element = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%">each</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">($prices) {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $element</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">[&apos;key&apos;]</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo &quot;-&quot;;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $element</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">[&apos;value&apos;]</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo &quot;&lt;br /&gt;&quot;;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">each()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">返回数组的当前元素，并将下一个元素作为当前元素。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">位置</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">key</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">和</span><span style="font-family: Times New Roman; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">0</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">包含了当前元素的关键字，而位置</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">value</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">和</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">1</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">包含了当前元素的值。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">list()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，将一个数组分解为一系列的值。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">while( </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">list</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">($product, $price) = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">each</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">($prices) ) {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo &quot;$product - $price&lt;br /&gt;&quot;;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">reset()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">函数，将当前元素重新设置到数组开始处。（使用</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">each</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">后当前元素位置会变化。）</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.4 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">数组操作符</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">(p60)</span></div>
<div>
<ol>
<li><span style="font-family: Consolas; font-size: 12pt; color: #ff0000">+</span><span style="font-family: 宋体; font-size: 12pt">，</span><span style="font-family: Consolas; font-size: 12pt"> </span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff00ff">联合</span><span style="font-family: 宋体; font-size: 12pt">，如</span><span style="font-family: Consolas; font-size: 12pt">$a + $b</span><span style="font-family: 宋体; font-size: 12pt">，</span><span style="font-family: Consolas; font-size: 12pt">$b</span><span style="font-family: 宋体; font-size: 12pt">将被附加到</span><span style="font-family: Consolas; font-size: 12pt">$a</span><span style="font-family: 宋体; font-size: 12pt">中。</span><span style="font-family: Consolas; font-size: 12pt">$b</span><span style="font-family: 宋体; font-size: 12pt">与</span><span style="font-family: Consolas; font-size: 12pt">$a</span><span style="font-family: 宋体; font-size: 12pt">中索引相同(关键字冲突)的元素不添加。</span></li>
<li><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">==</span><span style="font-family: 宋体; font-size: 12pt">，等价，</span><span style="font-family: Consolas; font-size: 12pt">$a == $b, </span><span style="font-family: 宋体; font-size: 12pt">包含相同元素则返回</span><span style="font-family: Consolas; font-size: 12pt">true</span></li>
<li><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">===</span><span style="font-family: Consolas; font-size: 12pt">, </span><span style="font-family: 宋体; font-size: 12pt">恒等，</span><span style="font-family: Consolas; font-size: 12pt"> </span><span style="font-family: 宋体; font-size: 12pt">包含相同的顺序和类型的元素</span></li>
<li><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">!=</span><span style="font-family: Consolas; font-size: 12pt">, </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">&lt;&gt;</span><span style="font-family: Consolas; font-size: 12pt">, </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000">!==</span></li></ol>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.5 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">多维数组</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">定义</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">$products = </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #2f5496; padding-left: 21pt">array</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">( </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt">array</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">( &apos;TIR&apos;, &apos;Tires&apos;, 100), </span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt">array</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt"> ( &apos;OIL&apos;, &apos;Oil&apos;, 10), </span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 240%">array</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt; line-height: 240%"> ( &apos;SPK&apos;, &apos;Spark Plugs&apos;, 4) );</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">访问：</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">products[0][0]</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">还可利用双重</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">for</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">循环</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">使用</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">列名称</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">来代替数字索引：</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">(</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">即行数字索引，列是关联索引</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">)</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">$products = array ( </span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%">array</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #ff0000; padding-left: 21pt; line-height: 160%"> </span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #c45911; padding-left: 21pt; line-height: 160%">( &apos;Code&apos;=&gt;&apos;TIR&apos;, </span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Description&apos;=&gt;&apos;Oil&apos;, </span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&apos;Price&apos;=&gt;10 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;),</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 21pt; line-height: 160%">……</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">);</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 170%">访问方式</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">：行数字索引</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">+</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">列名称索引</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">$products</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt; line-height: 240%">[$</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">row</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt; line-height: 240%">][</span><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 240%">&apos;Code&apos;</span><span style="font-family: Consolas; font-size: 11pt; color: #2f5496; padding-left: 21pt; line-height: 240%">]</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">三维数组</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">：高、宽、深；</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">php</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">数组</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; line-height: 240%">没有维数限制</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.6 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">数组排序</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">3.6.1 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">使用</span><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">sort()</span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">函数</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">对数组</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">products</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">进行按字母</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">/</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">数字升序排序：</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">sort</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($products);</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">sort()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">函数</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">区分字母大小写，大写字母在小写字母前面</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">默认按字母、数字升序排序</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">函数第二个参数可选，可为</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">SORT_REGULAR</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">(</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">默认值</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">), SORT_NUMERIC, SORT_STRING</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">3.6.2 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">使用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">asort()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">函数和</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">ksort()</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">函数对关联数组排序</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">关键字和值在排序时仍然</span><span style="font-family: 宋体; font-size: 12pt; text-decoration: underline; line-height: 170%">保持一致</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 160%">asort</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，根据数组的每一个元素值进行排序。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 40pt; line-height: 240%">ksort</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">()</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">，根据数组的</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 240%">key</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">即关键字进行排序</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">3.6.3 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">反向排序</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 240%">rsort()</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">, </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">arsort</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">(), </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">krsort</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，与</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">sort(), assort(), ksort()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">相反，将数组降序排序。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.7 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">多维数组的排序（</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">p64</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">）</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">usort()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，告诉</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">PHP</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">如何比较各个元素。默认按照</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">compare</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">所定义的升序来排列。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">usort($products, &apos;compare&apos;);</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #ff0000; padding-left: 21pt; line-height: 160%">function</span><span style="font-family: Consolas; font-size: 11pt; color: #ff0000; padding-left: 21pt; line-height: 160%"> </span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">compare($x, $y){</span></div>
<div><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%">if ($x[1] == $y[1]) {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #002060; padding-left: 21pt; line-height: 160%">return 0</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;} else if ($x[1] &lt; $y[1]) {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #002060; padding-left: 21pt; line-height: 160%">return -1</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;} else {</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #002060; padding-left: 21pt; line-height: 160%">return 1</span><span style="font-family: Consolas; font-size: 11pt; color: #c45911; padding-left: 21pt; line-height: 160%">;</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 160%"> &nbsp; &nbsp; &nbsp; &nbsp;}</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; padding-left: 21pt; line-height: 240%">}</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">对应的</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 240%">uasort()</span><span style="font-family: Consolas; font-size: 12pt; line-height: 240%">, </span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 240%">uksort()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，要求传入用户定义的比较函数。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.8 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">对数组进行重新排序（</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">p66</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">）</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">shuffle()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，将数组各元素进行随机排序。修改原数组。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">array_reverse()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，给出一个原来数组的反向排序。</span><span style="font-family: 宋体; font-size: 12pt; text-decoration: underline; line-height: 170%">返回原数组修改后的副本</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，可直接用来覆盖原数组。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">array_push()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%"> </span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">将每个新元素添加到数组的末尾。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">array_pop()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">，删除并返回数组末尾的一个元素。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.9 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">从文件载入数组</span><span style="font-family: Calibri; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">(p68)</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">count()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，统计数组中元素的个数，利用</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 170%">file</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数载入文件到数组。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">explode()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">函数，将传入的字符串分割成一个个小块。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">array explode(string separator, string string [, int limit])</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 240%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">参数</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 240%">limit</span><span style="font-family: Times New Roman; font-size: 11pt; padding-left: 40pt; line-height: 240%">,</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 240%">限制被返回的最大块数</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 240%">intval()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 240%">函数，将一个字符串转化成整数，即从中提取数字，忽略不能转化的部分。</span></div>
<div><br /></div>
<div><span style="font-family: 微软雅黑; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">3.10 &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16pt; font-weight: bold; padding-left: 70pt; line-height: 170%">执行其它的数组操作</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">3.10.1 </span><span style="font-family: 宋体; font-size: 12pt; color: #000000; line-height: 160%">在数组中浏览</span><span style="font-family: Consolas; font-size: 12pt; color: #000000; line-height: 160%">(p71)</span></div>
<div><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">数组内部指针：指向数组中的当前元素。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">current</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($array_name)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，指向当前元素，同</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">pos</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">()</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">each</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($array_name)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，在指针前移一个位置之前返回当前元素。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">next</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($array_name)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，将指针前移，然后再返回新的当前元素。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">reset</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($array_name)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，返回指向数组第一个元素的指针，指针移到开头。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">end</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($array_name)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，返回数组的最后一个元素，指针移到末尾。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">prev</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($array_name)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，将当前指针往回移一个位置，然后再返回新的当前元素。</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">pos</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">($array_name), Alias of current</span></div>
<div><br /></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">3.10.2 </span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">对数组每个元素应用函数</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">(p72)</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">array_walk</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，以相同方式使用或者修改数组中的每一个元素。</span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt">bool array_walk(array arr, string func, [mixed userdata])</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">func</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，用户自定义的作用于每个元素的函数</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">userdata</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，可以作为一个参数传递给自己的函数</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; line-height: 170%">yourfunction(value, key, userdata)</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，用户自定义的函数，可以忽略关键字参数和</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">userdata</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">参数。</span></div>
<div><br /></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">3.10.3 </span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">统计数组元素个数</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">count</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，对数组中元素的个数进行统计</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">sizeof</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，同</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">count()</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #ff0000; line-height: 170%">array_count_values</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%">()</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">统计每个特定的值在数组中出现过的次数（数组的基数值）。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">返回一个包含频率表的关联数组。（以值作为关联数组的关键字）。</span></div>
<div><br /></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-weight: bold; color: #000000; line-height: 160%">3.10.4 </span><span style="font-family: 宋体; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">将数组转换成标量变量</span><span style="font-family: Consolas; font-size: 12pt; font-weight: bold; color: #000000; line-height: 160%">(P74)</span></div>
<div><span style="font-family: Consolas; font-size: 12pt; color: #ff0000; line-height: 170%">extract()</span><span style="font-family: 宋体; font-size: 12pt; line-height: 170%">，创建数组对应的标量变量，针对非数字索引数组</span><span style="font-family: Consolas; font-size: 12pt; line-height: 170%"> </span></div>
<div><span style="font-family: Consolas; font-size: 11pt; font-style: italic; color: #2f5496; padding-left: 21pt; line-height: 190%">extract(array var_array [, int extract_type] [, string prefix] );</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">标量变量的名称</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">必须是数组中关键字的名称，</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">值</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">则是数组中的值。</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">extract_type, </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">如何处理名字冲突，</span><span style="font-family: 宋体; font-size: 11pt; font-weight: bold; padding-left: 40pt; line-height: 160%">默认覆盖</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">已有变量</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">(p74</span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">，表</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">3-2)</span></div>
<div><span style="font-family: 宋体; font-size: 10pt; font-weight: bold; padding-left: 40pt; line-height: 160%">* &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Consolas; font-size: 11pt; padding-left: 40pt; line-height: 160%">prefix, </span><span style="font-family: 宋体; font-size: 11pt; padding-left: 40pt; line-height: 160%">冲突时可能需要的参数</span></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><br /></div>
<div><span style="font-family: Times New Roman; font-size: 12pt">
</span></div>
<div><br /></div></div></div></div></div></div></div></div><script type="text/javascript" language="javascript" src="jquery.js"></script><script type="text/javascript" language="javascript" src="itemlink.js"></script></body></html>